//
//  CustomView.m
//  图形上下文绘图
//
//  Created by 伍银荣 on 2021/6/30.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "CustomView.h"

@implementation CustomView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    [self test5];
    
}

//第五种:oc,最简单的一种
- (void)test5 {
    //拼接路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 100)];
    //将路径添加到上下文中系统已经帮忙做好了.
    
    //渲染
    [path stroke];
}

//第四种:C语言 + OC的写法
- (void)test4 {
    //1.获取当前上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.拼接路径(C语言)
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 50, 50);
    CGPathAddLineToPoint(path, NULL, 100, 100);
    
    //拼接路径(OC)
    UIBezierPath *path1 = [UIBezierPath bezierPathWithCGPath:path];
    [path1 addLineToPoint:CGPointMake(150, 50)];
    
    //把路径添加到上下文中
    CGContextAddPath(ctx, path1.CGPath);
    
    //渲染(将上下文中的图像绘制到对应的设备上)
    CGContextStrokePath(ctx);
    
}

//第三种:C语言 + OC的写法
- (void)test3 {
   //2.获取当前图形上下文对象
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.使用OC拼接路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 100)];
    
    //3.C语言将拼接好的路径添加到图形上下文中. path.CGPath:将OC的路径转化成C语言的
    CGContextAddPath(ctx, path.CGPath);
    
    //渲染
    CGContextStrokePath(ctx);
    
}

//第二种:纯C语言的写法 + CGMutablePathRef的写法
- (void)test2 {
    //1.获取图形上下文对象
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.创建可变路径
    CGMutablePathRef path = CGPathCreateMutable();
    //3.拼接路径
    CGPathMoveToPoint(path, NULL, 50, 50);
    CGPathAddLineToPoint(path, NULL, 100, 100);
    CGPathAddLineToPoint(path, NULL, 150, 50);
    //4.将路径添加到图形上下文对象中
    CGContextAddPath(ctx, path);
    //5.渲染(将图形上下文对象的图像绘制到对应的设备上)
    CGContextStrokePath(ctx);
}


//第一种:纯C语言的写法
- (void)test1 {
    //1.获取图形上下文对象(Layer)
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.拼接路径,同时将路径添加到图像上下文对象中
    //此时画笔是悬空的状态,只是将画笔放在这个点的上空
    CGContextMoveToPoint(ctx, 50, 50);
    //此时将画笔放在上一个点上,然后划到这个点上,就画出了一条线
    CGContextAddLineToPoint(ctx, 100, 100);
    //此时画笔由上一个点划到(150, 50)的这个点
    CGContextAddLineToPoint(ctx, 150, 50);
    
    CGContextMoveToPoint(ctx, 50, 150);
    CGContextAddLineToPoint(ctx, 150, 150);
    
    //3.渲染(将图形上下文对象中的图形绘制到对应的设备上)
    CGContextStrokePath(ctx);
}


@end
